import BaseController from '../../core/controllers/Base.controller';
import storageApi from '../../core/storage/storage';

@Inject('$log', '$state', '$rootScope', 'ngDialog', 'globalService', 'ngDialogService', 'postionService', 'departmentService', 'deviceCategoryService', 'deviceUsedLocationService')
class systemManageController extends BaseController {
  constructor() {
    super();

    this.data = {
      type: -1,
      tableList: [],
    };
    this.data.orgId = this.$rootScope.orgId || storageApi.get('orgId');
    // 2部门 3岗位 4设备类型  5设备使用部位
    if ($state.current.name === 'department') {
      this.data.type = 2;
      this.data.typeName = '部门';
      this.paginationService = departmentService;
    }
    if ($state.current.name === 'postion') {
      this.data.type = 3;
      this.data.typeName = '岗位';
      this.paginationService = postionService;
    }
    if ($state.current.name === 'deviceCategory') {
      this.data.type = 4;
      this.data.typeName = '设备类型';
      this.paginationService = deviceCategoryService;
    }
    if ($state.current.name === 'deviceUsedLocation') {
      this.data.type = 5;
      this.data.typeName = '设备使用部位';
      this.paginationService = deviceUsedLocationService;
    }

    this.$setQueryCallback((res) => {
      this.$log.debug(res);
      this.data.tableList = res.rows;
    });

    this.query();
    this.querySystemDicByType();
  }

  query() {
    this.mapParams.orgId = this.$rootScope.orgId || storageApi.get('orgId');
    this.$paginationQuery();
  }

  delete(activity) {
    this.ngDialogService.confirm('确定删除此数据吗？', activity)
      .then((data) => {
        this.paginationService.delete(data.id)
        .then((res) => {
          if (res.success) {
            this.query();
            this.ngDialogService.alertError(res.message);
          } else {
            this.ngDialogService.alertError('删除失败');
          }
        });
      });
  }

  reset() {
    this.mapParams = {};
  }

  querySystemDicByType() {
    this.globalService.querySystemDicByType('部门类别')
    .then((res) => {
      this.data.deptTypes = res.rows;
    });
  }

  openAddItemDialog(sysCtrl) {
    this.ngDialog.open({
      template: 'paAddItemDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller($scope, $log, ngDialogService) {
        'ngInject';

        this.item = {
          name: '',
          code: '',
          typeCodeName: '',
          orgId: sysCtrl.data.orgId,
        };
        this.parentData = sysCtrl.data;

        this.submitForm = () => {
          sysCtrl.paginationService.save(this.item)
            .then((res) => {
              if (res.success) {
                ngDialogService.alertSuccess('新增成功');
                sysCtrl.query();
                $scope.closeThisDialog();
              } else {
                ngDialogService.alertError(res.message);
              }
            });
        };
      },
    });
  }

  openEditItemDialog(value, sysCtrl) {
    this.ngDialog.open({
      template: 'paEditItemDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller: function ctr($log, $scope, ngDialogService) {
        'ngInject';

        this.item = angular.copy(value);
        this.parentData = sysCtrl.data;

        this.submitForm = () => {
          sysCtrl.paginationService.update(this.item)
          .then((res) => {
            if (res.success) {
              ngDialogService.alertSuccess('修改成功');
              $scope.closeThisDialog();
              sysCtrl.query();
            } else {
              ngDialogService.alertError(res.message);
            }
          });
        };
      },
    });
  }

}
export default systemManageController;
